﻿using BMSPlatform.ViewModel;
using DataManage;
using ServiceLayerManagement.Tool;
using ServiceLayerManagement;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using System;

namespace BMSPlatform.View
{
    /// <summary>
    /// ApplicationUpgradeView.xaml 的交互逻辑
    /// </summary>
    public partial class ApplicationUpgradeView : UserControl
    {
        public ApplicationUpgradeViewModel VM => this.DataContext as ApplicationUpgradeViewModel;
        MainWindow mainWindow;
        public ApplicationUpgradeView()
        {
            InitializeComponent();
            Init();
        }

        public void Init()
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    AddInfo();
                    ThreadHelper.Delay(100);
                }
            });
            VM.UpgradeEnableAction += UpgradeEnableChanged;
        }
        private void SystemUpgrade_Click(object sender, RoutedEventArgs e)
        {
            mainWindow = Window.GetWindow(this) as MainWindow;
            VM.OnSystemUpgrade();
        }

        private void SelectedFilePath_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "BIN文件(.bin)|*.bin";
                if (openFileDialog.ShowDialog() == true)
                {
                    VM.BinFilePath = openFileDialog.FileName;
                    VM.BinData = VM.ReadBinFile();
                    VM.BinFileParse();
                }
            }
            catch(Exception ex)
            {
                MessageHelper.Error(ex.Message);
            }
        }

        public void AddInfo()
        {
            double i;
            string info;
            AppHelper.RunInUI(this, () =>
            {
                if (VM.ProgressBarValueQueue != null && VM.ProgressBarValueQueue.TryDequeue(out i))
                {
                    VM.ProgressBarCurrentValue = (VM.ProgressBarCurrentValue + (i / VM.ProgressBarMaxValue) * 100);
                }
                if (VM.MessageQueue != null && VM.MessageQueue.TryPeek(out info)
                    && ((!info.Contains("数据烧写失败") && !info.Contains("数据烧写完毕") && !info.Contains("CRC") && !info.Contains("APP")) || VM.ProgressBarValueQueue.Count <= 0))
                {
                    if (VM.MessageQueue.TryDequeue(out info))
                    {
                        VM.ProcessInfos.Add(info);
                        if (nListView.Items.Count > 1) nListView.ScrollIntoView(nListView.Items[nListView.Items.Count - 1]);
                    }
                }
            });
        }
        public void UpgradeEnableChanged(bool b)
        {
            AppHelper.RunInUI(this, () => 
            {
                VM.UpgradeEnable = b;
                if (mainWindow != null) mainWindow.VM.IsControlEnable = b;
            });
        }
    }
}
